package com.example.myzhugezhaofang.adapter.newhouse;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mvplibrary.adapter.BaseAdapter;
import com.example.mvplibrary.adapter.BaseViewHolder;
import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.bean.newhouse.HouseListBean;
import com.example.myzhugezhaofang.custom.TextViewBorder;

import java.util.ArrayList;
import java.util.List;

public class HouseListAdapter extends BaseAdapter<HouseListBean.DataBean.DataBeans> {
    public onItemListener itemListener;

    public void setListener(onItemListener listener) {
        this.itemListener = listener;
    }

    public HouseListAdapter(List<HouseListBean.DataBean.DataBeans> datas, Context context, int layoutId) {
        super(datas, context, layoutId);
    }

    @Override
    protected void bindData(BaseViewHolder holder, HouseListBean.DataBean.DataBeans listBean, int position) {
        ArrayList<HouseListBean.DataBean.DataBeans.TagBean> tagList = new ArrayList<>();
        for (int i = 2; i < listBean.getTag().size(); i++) {
            HouseListBean.DataBean.DataBeans.TagBean tagBean = listBean.getTag().get(i);
            tagList.add(tagBean);
        }
        TextView text11 = holder.getView(R.id.tv_item_houseList_11);
        TextViewBorder text12 = holder.getView(R.id.tv_item_houseList_12);
        TextView text21 = holder.getView(R.id.tv_item_houseList_21);
        TextView text22 = holder.getView(R.id.tv_item_houseList_22);
        TextView text3 = holder.getView(R.id.tv_item_houseList_3);
        RecyclerView rv_list_tag = holder.getView(R.id.rv_tag_houseList_4);
        TextView text51 = holder.getView(R.id.tv_item_houseList_51);
        TextView text52 = holder.getView(R.id.tv_item_houseList_52);

        ImageView big = holder.getView(R.id.iv_item_houseList);
        ImageView small = holder.getView(R.id.iv_bg_houseList);

        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_list_tag.setLayoutManager(manager);
        text11.setText(listBean.getName());

        String color1 = listBean.getNew_tag().get(0).getTitleColor();
        int textColor = Color.parseColor(color1);
        String color2 = listBean.getNew_tag().get(0).getBgColor();
        int borderColor = Color.parseColor(color2);
        text12.setText(listBean.getNew_tag().get(0).getName());
        text12.setTextColor(textColor);
        text12.setBackgroundColor(borderColor);
        if (listBean.getPrice().getNounit_price().equals("0")) {
            text21.setText(listBean.getPrice().getPrice());
        } else {
            text21.setText(listBean.getPrice().getNounit_price());
        }
        text22.setText(listBean.getPrice().getUnit());

        TagsAdapter tagAdapter = new TagsAdapter(context, tagList);
        rv_list_tag.setAdapter(tagAdapter);

        text3.setText(listBean.getRegion() + " " + listBean.getCateCircle() + " | " + listBean.getJianmian_taonei() + " " + listBean.getHouse_area());
        if (listBean.getWords_list().size() > 1 && listBean.getWords_list().size() != 0) {
            text51.setText(listBean.getWords_list().get(1).getName());
            text52.setText(listBean.getWords_list().get(1).getTitle());
            Glide.with(context).load(listBean.getWords_list().get(1).getNew_img_url()).into(small);
        } else if (listBean.getWords_list().size() == 1 && listBean.getWords_list().size() != 0) {
            text51.setText(listBean.getWords_list().get(0).getName());
            text52.setText(listBean.getWords_list().get(0).getTitle());
            Glide.with(context).load(listBean.getWords_list().get(0).getNew_img_url()).into(small);
        }
        Glide.with(context).load(listBean.getThumb()).into(big);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemListener.onClick(position);
            }
        });
    }

    public interface onItemListener {
        void onClick(int n);
    }
}
